package com.example.unique.ui.chat

import android.annotation.SuppressLint
import android.database.Cursor
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.unique.database.DatabaseHelper


data class Member(
    val id: Int,
    val sno:String,
    val academy:String,
    val name:String
)
class ChatViewModel(private val databaseHelper: DatabaseHelper):ViewModel() {
    private val _membersData = MutableLiveData<List<Member>>()
    val membersData: LiveData<List<Member>> = _membersData
    @SuppressLint("Range")
    fun getUserByClub(club:String){
        val cursor: Cursor = databaseHelper.getUserByClub(club)
        if (cursor.count == 0) {
            println("No users found for club: $club")
        } else {
            println("Found ${cursor.count} users for club: $club")
        }

        val members = mutableListOf<Member>()

        if (cursor.moveToFirst()) {
            do{
                val member = Member(
                    id = cursor.getInt(cursor.getColumnIndex("id")),
                    academy = cursor.getString(cursor.getColumnIndex("academy")),
                    name = cursor.getString(cursor.getColumnIndex("name")),
                    sno = cursor.getString(cursor.getColumnIndex("sno")),
                )
                members.add(member)
            }while (cursor.moveToNext())
        }
        cursor.close()
        _membersData.value=members
    }
}